/**
 * Copyright 2010 Butin Kirill <kiryaka@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.flexupload.status
{
	import flash.net.FileReference;
	
	import org.flexupload.Flexupload;
	import org.flexupload.event.FlexuploadEvent;
	import org.flexupload.event.UploadStatusEvent;
	
	public class UploadStatus extends StatusBase
	{
		private var	totalBytes:uint = 0;
		
		public function UploadStatus(){}
		public function start():void
		{
			startCounting();
		}
		override public function updateBytesTransfered(bytesTransfered:int):void{
			super.updateBytesTransfered(bytesTransfered);
			if (isSpamSafety(getCurrentTimeStamp() ) ) return;
			dispatchEvent(UploadStatusEvent.AVERAGE_SPEED_UPDATE); // todo global events system
			dispatchEvent(UploadStatusEvent.UPLOADED_SIZE_CHANGE); // todo global events system
		}
		private function dispatchEvent(type:String):void{
			var event:UploadStatusEvent = new UploadStatusEvent(type);
			event.setUploadStatus(this);
			Flexupload.getInstance().dispatchEvent(event);
		}
		public function startUpload():void{
			dispatchEvent(UploadStatusEvent.UPLOAD_START);
		}
		public function finishUpload():void{
			dispatchEvent(UploadStatusEvent.UPLOAD_FINISH);
		}		
		public function addNewFile(file:FileReference):void{
			totalBytes += file.size;
			dispatchEvent(UploadStatusEvent.TOTAL_SIZE_CHANGE);
			dispatchFileEvent(FlexuploadEvent.FILE_ADD, file);
		}
		private function dispatchFileEvent(type:String, file:FileReference):void{
			var event:FlexuploadEvent = new FlexuploadEvent(type);
			event.setFile(file);
			Flexupload.getInstance().dispatchEvent(event);
		}		
		public function getTotalBytes():int{
			return totalBytes;
		}
		public function finish():void{
			totalBytes = 0;
			bytesTransfered = 0;
			timeStart = 0;
			currentTimeStamp = 0;
			lastTimeStamp = 0;			
		}
	}
}